﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;

/*
 * TH07 Groep 10
 * ----------------------------------------------------------------------------------
 * Versie   Door        Commentaar
 * 0.1      ?????       Opzet en programmeer werk
 * 0.2      Erwin       Verplaatst naar Algorithms, 
 *                      aangepast om als reference te gebruiken
 */

namespace Algorithms.Filters
{
    public class Inverse
    {
        public static Bitmap RunAlgorithm(Bitmap source)
        {
            int _width = source.Width;
            int _height = source.Height;

            Bitmap output = new Bitmap(_width, _height);
            BitmapData outputData = output.LockBits(
                new Rectangle(0, 0, _width, _height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
            BitmapData sourceData = source.LockBits(
                new Rectangle(0, 0, _width, _height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            int totalWidth = _width * 3;
            int srcOffset = sourceData.Stride - totalWidth;
            int optOffset = outputData.Stride - totalWidth;

            unsafe
            {
                byte* srcPnt = (byte*)sourceData.Scan0;
                byte* optPnt = (byte*)outputData.Scan0;

                for (int y = 0; y < _height; y++)
                {
                    for (int x = 0; x < _width; x++)
                    {
                        optPnt[0] = (byte)(255 - srcPnt[0]);
                        optPnt[1] = (byte)(255 - srcPnt[1]);
                        optPnt[2] = (byte)(255 - srcPnt[2]);
                        srcPnt += 3;
                        optPnt += 3;
                    }

                    srcPnt += srcOffset;
                    optPnt += optOffset;
                }
            }
            source.UnlockBits(sourceData);
            output.UnlockBits(outputData);
            return output;
        }
    }
}
